package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kittlen.managementSystem.entity.Admin;
import com.kittlen.managementSystem.entity.Coach;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Result;
import com.kittlen.managementSystem.service.IAdminService;
import com.kittlen.managementSystem.utils.Md5;
import com.kittlen.managementSystem.utils.ObjectUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    IAdminService iAdminService;

    /**
     * 管理员登陆
     * @param userId
     * @param pwd
     * @return
     */
    @RequestMapping("/login")
    public Result login(String userId, String pwd, HttpServletRequest request){
        Result result=new Result();
        Admin admin=iAdminService.getById(userId);
        if(ObjectUtil.isNoEmpty(admin)){
            if(pwd.equals(admin.getPwd())){
                admin.setPwd("");
                result.setData(admin);
                request.getSession().setAttribute("user",admin);
                request.getSession().setAttribute("type",9);
                return result;
            }
        }
        result.setCode(result.RESULT_ERROR);
        result.setMsg("账号或密码错误");
        return result;
    }

    /**
     * 管理员查询
     * @param admin
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selects")
    public Result selects(Admin admin,@RequestParam(required = false,defaultValue = "0")int page,
                          @RequestParam(required = false,defaultValue = "0")int limit){
        Result result=new Result();
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNoEmpty(admin.getAdminId())){
            queryWrapper.like("admin_id", admin.getAdminId());
        }
        if(ObjectUtil.isNoEmpty(admin.getFullname())){
            queryWrapper.like("fullname", admin.getFullname());
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
//        List<Admin> list=iAdminService.selects(queryWrapper);
        List<Admin> list=iAdminService.list(queryWrapper);
        result.setData(list);
        return result;
    }

    /**
     * 管理员添加
     * @param admin
     * @return
     */
    @RequestMapping("/insert")
    public Result insert(Admin admin){
        Result result=new Result();
        if(iAdminService.save(admin)) {
            result.setData(admin);
            result.setMsg("管理员添加成功");
        }else{
            result.setCode(result.RESULT_ERROR);
            result.setMsg("管理员添加失败");
        }
        return result;
    }

    /**
     * 管理员修改
     * @param admin
     * @return
     */
    @RequestMapping("/update")
    public Result update(Admin admin){
        Result result=new Result();
        if(iAdminService.updateById(admin)){
            result.setData(admin);
            result.setMsg("管理员信息修改成功");
        }else{
            result.setCode(result.RESULT_ERROR);
            result.setMsg("管理员信息修改失败");
        }
        return result;
    }

    /**
     * 修改密码
     * @param
     * @return
     */
    @RequestMapping("/updatePwd")
    public Result updatePwd(String adminId,@RequestParam(required = false,defaultValue = "")String newPwd,@RequestParam(required = false,defaultValue = "")String oldPwd){
        Result result=new Result();
        if(ObjectUtil.isNoEmpty(newPwd)&&ObjectUtil.isNoEmpty(oldPwd)){
            Admin admin=iAdminService.getById(adminId);
            if(ObjectUtil.isNoEmpty(admin)){
                if(admin.getPwd().equals(oldPwd)){
                    iAdminService.update(new UpdateWrapper<Admin>().set("pwd",newPwd).eq("admin_id",adminId));
                    result.setMsg("密码修改成功,请重新登陆");
                }else{
                    result.setMsg("密码修改失败,原密码错误");
                    result.setCode(result.RESULT_ERROR);
                }
            }else{
                result.setMsg("账号信息异常,请重新登陆");
                result.setCode(result.RESULT_ERROR);
            }
        }else{
            try {
                Admin admin=new Admin(adminId);
                admin.setPwd(Md5.UseMD5("123456"));
                iAdminService.updateById(admin);
                result.setMsg("密码重置成功");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 管理员删除
     * @param admin
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Admin admin){
        Result result=new Result();
        if(iAdminService.removeById(admin)) {
            result.setMsg("删除成功");
        }else{
            result.setMsg("删除失败");
            result.setCode(result.RESULT_ERROR);
        }
        return result;
    }
}
